home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 10 / FM Towns Free Software Collection 10.iso / ms_dos / tool / fapxtool / src / txl / txlbtm.c < prev    next >
C/C++ Source or Header  |  1995-02-14  |  3KB  |  139 lines

  1. /***************
  2. *
  3. * g:\exe\txl\src\txlbtm.c
  4. */
  5. #include "txl.h"
  6.  
  7. void bottomcut(char *val[])
  8. {
  9.     FILE *fp, *fr;
  10.     int  minus, cnt = 127;
  11.     long tsize;
  12.     char *offs, *line;
  13.     char *tname = "~TXLTMP~.$$$";
  14.     struct stat stbuf;
  15.  
  16.     fprintf(stderr, "TXL: BOTTOM EXTRACTER\n");
  17.     if (stat(val[0], &stbuf)) {
  18.         fprintf(stderr, "TXL:no exist '%s'", val[0]);
  19.         Exit(1);
  20.     }
  21.     if (stbuf.st_size < 127) {
  22.         cnt = stbuf.st_size;
  23.     }
  24.     if ((line = calloc(cnt + 1, 1)) == NULL) {
  25.         errexit("out of memory");
  26.     }
  27.     if ((fp = fopen(val[0], "rb")) == NULL) {
  28.         free(line);
  29.         fprintf(stderr, "TXL:can't open '%s'", val[0]);
  30.         Exit(1);
  31.     }
  32.     fseek(fp, -(cnt), SEEK_END);
  33.     fread(line, 1, cnt, fp);
  34.     if (line[cnt - 1] == 0x0a) {
  35.         line[cnt - 2] = NUL;
  36.     }
  37.     if (line[cnt - 1] == 0x1a) {
  38.         if (line[cnt - 2] == 0x0a) {
  39.             line[cnt - 3] = NUL;
  40.         }
  41.     }
  42.     if ((offs = strrchr(line, '\n')) == NULL) {
  43.         fclose(fp);
  44.         free(line);
  45.         errexit("bottom line is too long. (more than 80 bytes?)");
  46.     }
  47.     minus = line + cnt - (offs + 1);
  48.     fclose(fp);
  49.     free(line);
  50.     if ((line = malloc(32767)) == NULL) {
  51.         errexit("out of memory");
  52.     }
  53.  
  54.     strcpy(line1, val[0]);
  55.     if ((offs = jstrrchr(line1, '\\')) != NULL) {
  56.         strcpy(offs+1, tname);
  57.     }
  58.     else if ((offs = jstrrchr(line1, ':')) != NULL) {
  59.         strcpy(offs+1, tname);
  60.     }
  61.     else {
  62.         strcpy(line1, tname);
  63.     }
  64.     rename(val[0], line1);
  65.     tsize = stbuf.st_size - minus;
  66.     if ((fp = fopen(val[0], "wb")) == NULL) {
  67.         free(line);
  68.         fprintf(stderr, "TXL:can't open '%s'", val[0]);
  69.         Exit(1);
  70.     }
  71.     if ((fr = fopen(line1, "rb")) == NULL) {
  72.         free(line);
  73.         fprintf(stderr, "TXL:can't open '%s'", line1);
  74.         Exit(1);
  75.     }
  76.     for (; tsize > 0; tsize -= 32767) {
  77.         cnt = ((tsize > 32767) ? 32767 : tsize);
  78.         cnt = fread(line, 1, cnt, fr);
  79.         fwrite(line, 1, cnt, fp);
  80.     }
  81.     fclose(fp);
  82.     fclose(fr);
  83.     free(line);
  84.     remove(line1);
  85.     Exit(0);
  86. }
  87.  
  88. void bottomdsp(char *val[], int level)
  89. {
  90.     FILE *fp;
  91.     char *offs, *line;
  92.     struct stat stbuf;
  93.     int cnt = 255;
  94.  
  95.     fprintf(stderr, "TXL: BOTTOM EXTRACTER\n");
  96.     if (stat(val[0], &stbuf)) {
  97.         fprintf(stderr, "TXL:no exist '%s'", val[0]);
  98.         Exit(1);
  99.     }
  100.     if (stbuf.st_size < 255) {
  101.         cnt = stbuf.st_size;
  102.     }
  103.     if ((line = calloc(cnt + 1, 1)) == NULL) {
  104.         errexit("out of memory");
  105.     }
  106.     if ((fp = fopen(val[0], "rb")) == NULL) {
  107.         free(line);
  108.         fprintf(stderr, "TXL:can't open '%s'", val[0]);
  109.         Exit(1);
  110.     }
  111.     fseek(fp, -(cnt), SEEK_END);
  112.     fread(line1, 1, cnt, fp);
  113.     if (line1[cnt - 1] == 0x0a) {
  114.         line1[cnt - 2] = NUL;
  115.     }
  116.     if (line1[cnt - 1] == 0x1a) {
  117.         if (line1[cnt - 2] == 0x0a) {
  118.             line1[cnt - 3] = NUL;
  119.         }
  120.     }
  121.     if ((offs = strrchr(line1, '\n')) == NULL) {
  122.         fclose(fp);
  123.         free(line);
  124.         errexit("bottom line is too long. (more than 80 bytes)");
  125.     }
  126.     if (level > 1) {
  127.         *(offs - 1) = NUL;
  128.         if ((offs = strrchr(line1, '\n')) == NULL) {
  129.             fclose(fp);
  130.             free(line);
  131.             errexit("before bottom line is too long. (more than 80 bytes)");
  132.         }
  133.     }
  134.     fputs(offs + 1, fpmes);
  135.     fputc('\n', fpmes);
  136.     free(line);
  137.     Exit(0);
  138. }
  139.